package com.qf.hospital.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.entity.SysRole;
import com.qf.hospital.entity.SysRoleExample;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.SysRoleMapper;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.service.SysRoleService;
import com.qf.hospital.util.ExampleUtil;
import com.qf.hospital.vo.LayUIDTreeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zjw
 * @description 角色信息的Service
 * @date 2021/10/18 14:45
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuService sysMenuService;

    private Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Override
    public List<SysRole> findAll() {
        return sysRoleMapper.selectByExample(null);
    }

    @Override
    public PageInfo<SysRole> loadAllRole(String rolename, String roledesc, Integer available, Integer page, Integer limit) {
        //1. 分页
        PageHelper.startPage(page,limit);

        //2. 条件
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(rolename)){
            criteria.andRoleNameLike(ExampleUtil.any(rolename));
        }
        if(!StringUtils.isEmpty(roledesc)){
            criteria.andRoleDescLike(ExampleUtil.any(roledesc));
        }
        if(available != null){
            criteria.andAvailableEqualTo(available);
        }

        //3. 查询
        List<SysRole> list = sysRoleMapper.selectByExample(example);

        //4. 封装
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);

        //5. 返回
        return pageInfo;
    }

    @Override
    public List<LayUIDTreeVO> initRoleMenuTreeJSON(Integer roleid) {
        //1. 查询全部的菜单信息
        List<SysMenu> sysMenuList = sysMenuService.findAll();

        //2. 查询当前角色拥有的菜单
        List<Integer> roleMenuIds = sysRoleMapper.findMenuIdByRoleId(roleid);

        //3. 封装数据 - 封装的VO，设置checkArr属性
        List<LayUIDTreeVO> data = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            LayUIDTreeVO vo = new LayUIDTreeVO();
            BeanUtils.copyProperties(sysMenu,vo);
            vo.setCheckArr(roleMenuIds.contains(vo.getId()) ? "1" : "0");
            data.add(vo);
        }

        //4. 返回
        return data;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveRoleMenu(Integer roleid, Integer[] ids) {
        //1. 先删除当前角色拥有的全部菜单信息
        sysRoleMapper.deleteRoleMenus(roleid);

        //2. 如果ids不为null并且长度大于0，批量添加角色的菜单信息
        if(ids != null && ids.length > 0){
            int count = sysRoleMapper.saveRoleMenu(roleid,ids);
            if(count != ids.length){
                log.error("【分配角色菜单】 分配角色菜单异常！ roleid = {}，ids = {}",roleid, Arrays.toString(ids));
                throw new HospitalException(HospitalExceptionEnums.ROLE_SAVE_ROLE_MENU_ERROR);
            }
        }
    }








}
